/*
 *  Copyright (C) 2010 fabien
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.boc.botv.web.controller;

import com.boc.botv.common.Database;
import com.boc.botv.service.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author fabien
 */
@Controller
public class LoginController {

  @Autowired
  private UserManager manager;

  @RequestMapping(value = "/login", method = RequestMethod.GET)
  public void loginSetUp() {
  }

  @RequestMapping(value = "/login", method = RequestMethod.POST)
  public String loginProcess(
          @RequestParam(value = "username", required = true) String login,
          @RequestParam(value = "password", required = true) String password,
          @RequestParam(value = "from", required = false) String from) {
    Database.startTransaction(true);
    if (from == null || from.equals("")) {
          from = "/index.htm";
    }
    manager.loqIn(login, password);
    Database.closeTransaction();
    return "redirect:" + from;
  }

  @RequestMapping(value = "/logout", method = RequestMethod.POST)
  public String logoutProcess(@RequestParam(value = "from", required = false) String from) {
    if (from == null || from.equals("")) {
      from = "/index.htm";
    }
    return "redirect:" + from;
  }
}
